package com.qf.business.system.controller;

import com.alibaba.fastjson.JSON;
import com.qf.business.system.service.RoleService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.system.dto.RoleDto;
import com.qf.data.system.dto.RoleDto2;
import com.qf.data.system.entity.Role;
import com.qf.data.system.vo.input.RoleInput;
import com.qf.data.system.vo.output.RoleOutput;
import com.qf.data.system.vo.output.RoleOutput2;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     * @return
     */
    @RequestMapping("/list")
    public R list(){
        List<RoleDto> roleDto = roleService.queryList();
        List<RoleOutput> roleOutputs = QfBeanUtils.listCopy(roleDto,RoleOutput.class);
         return Rs.create(roleOutputs);
    }

    @RequestMapping("/insert")
    public R insert(@Validated RoleInput roleInput){
        Role role =QfBeanUtils.beanCopy(roleInput, Role.class);
        roleService.save(role);
        return Rs.create("succ");
    }

    /**
     * 修改权限
     * @param rid
     * @param pidsStr
     * @return
     */
    @RequestMapping("/updatePower")
    public R updatePowersById(Long rid,String pidsStr){
        log.info("根据角色id 修改权限，{} - {}",rid,pidsStr);
        List<Long> pids = JSON.parseArray(pidsStr,Long.class);

        //调用service进行修改
        roleService.updatePowersByRid(rid,pids);
        return Rs.create("succ");
    }

    /**根据部门id查询出部门下的所有角色列表
     * 并且再根据员工的id，查询出该员工拥有哪些角色
     * 查询
     * @param depId
     * @param EmpId
     * @return
     */

    @RequestMapping("/queryByDepAndEmp")
    public R queryByDepAndEmp(Long depId,Long EmpId){
        List<RoleDto2> roleDto2 = roleService.queryRoleByDepAndEmp(depId, EmpId);
        List<RoleOutput2> roleOutput2s = QfBeanUtils.listCopy(roleDto2,RoleOutput2.class);
        return Rs.create(roleOutput2s);
    }

}
